home *** CD-ROM | disk | FTP | other *** search
- // fetch a surprise URL
- var yoonoRandomGlob = new Object();
- var win = yoonoGlob.WMED.getMostRecentWindow("navigator:browser");
- yoonoRandomGlob.browser = win.getBrowser().mCurrentBrowser;
-
- Components.utils.import("resource://yoono/yoonoService.js");
- Components.utils.import("resource://yoono/yoonoBkmSync.js");
- Components.utils.import("resource://yoono/yoonoLog.js");
-
- const YNPREFBRANCH = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.yoono.");
-
- yoonoRandomGlob.init = function() {
- try {
- var id = '';
- try {
- id = YNPREFBRANCH.getCharPref('randomfolderid');
- } catch(e) {
- }
-
- YNPREFBRANCH.setCharPref('randomfolderid', '');
- if(id != '') {
- YOONO_LOG.debug("Surprise from folder");
- yoonoRandomGlob.urlList = YOONO_BKM.getNodeListUrl(id);
- } else {
- YOONO_LOG.debug("Surprise from all bkms");
- yoonoRandomGlob.urlList = YOONO_BKM.getFullListUrl();
- }
-
- // display waiting message
- win.messageinfos.init('searching', win.yoonoGlob.gStrbundle.getString("random.searching"));
- // Get full bookmark list from bookmarks in order to check if random url is in bookmarks
- // (if it is, we'll take another one)
- // Absolute max number of requests to obtain random urls
- yoonoRandomGlob.maxRetry = 10;
- // try to read the maxRetry from prefs (it was given by the server at connect time and saved in prefs)
- try {
- yoonoRandomGlob.maxRetry = YNPREFBRANCH.getIntPref('surprise-me.max-retry');
- } catch(e) {} // in case not in prefs...
- if(yoonoRandomGlob.maxRetry > 10) yoonoRandomGlob.maxRetry = 10;
-
- // current retry
- yoonoRandomGlob.current = 0;
- // Hash table to easily find urls that are in bookmarks already
- yoonoRandomGlob.urlTable = {};
- for (var i = yoonoRandomGlob.urlList.length ; i-- > 0; ) {
- yoonoRandomGlob.urlTable[yoonoRandomGlob.urlList[i]] = 1;
- }
- // default server url
- yoonoRandomGlob.serverUrl = YNPREFBRANCH.getCharPref('serverurl') + 'linkserver';
- // Try getting server url from prefs, was given at connect time
- try {
- yoonoRandomGlob.serverUrl = YNPREFBRANCH.getCharPref('reco-service.url');
- } catch(e) {} // in case not in prefs...
-
- yoonoRandomGlob.getRandomSuggestions();
- } catch(e) {
- YOONO_LOG.exception(e);
- }
- }
- // Ask for suggestions from a random bookmark
- yoonoRandomGlob.getRandomSuggestions = function() {
- try {
- // Get a random offset in the list of bookmarks
- yoonoRandomGlob.randomOffset = Math.floor(Math.random() * yoonoRandomGlob.urlList.length);
- // Get the random bookmark. We'll use it to ask suggestions to the server
- yoonoRandomGlob.browser.ynSuggestUrlFrom = yoonoRandomGlob.urlList[yoonoRandomGlob.randomOffset];
- // If we must try again, we don't want to try this bookmark again. Remove it
- yoonoRandomGlob.urlList.splice(yoonoRandomGlob.randomOffset, 1);
- // count one try
- yoonoRandomGlob.current ++ ;
- var script = <server-script version="1.0"/>;
- script.appendChild( <compute-recommended-links url={yoonoRandomGlob.browser.ynSuggestUrlFrom} ranking="SURPRISEME"/>);
- YOONO_CMPT.sendRequest(yoonoRandomGlob.serverUrl, 'POST', 'async', script, yoonoRandomGlob.handleRandomSuggestions);
- } catch(e) {
- YOONO_LOG.exception(e);
- }
- }
-
- // Handler for server responses to suggestions from a random bookmark
- yoonoRandomGlob.handleRandomSuggestions = function(request, result) {
- try {
- var suggestUrl = '';
- if(result) {
- var suggestions = result['import-links'].link;
- // Get rid of suggestions that already are in our bookmarks or history
- var cleansuggestions = yoonoRandomGlob.ynCleanSuggestions (suggestions);
- // If some suggestions remain, pick up one at random
- if (cleansuggestions.length) {
- var link = cleansuggestions[(Math.floor(Math.random() * cleansuggestions.length))];
- //var link = cleansuggestions[0];
-
- suggestUrl = link.@url.toString();
- }
- }
- // Nothing is left, or bad answer : ask the server again (with another bookmark)
- // if we haven't tried all the bookmarks yet, and did not reach max retries
- if('' == suggestUrl) {
- if(yoonoRandomGlob.urlList.length && (yoonoRandomGlob.current < yoonoRandomGlob.maxRetry)) {
- yoonoRandomGlob.getRandomSuggestions();
- } else {
- // display no surprise found message
- win.messageinfos.init('randomfailed', win.yoonoGlob.gStrbundle.getString("random.failed"));
- // stat
- YOONO_CMPT.addStat(['nosurprise']);
- }
- } else {
- // keep track for message display
- yoonoRandomGlob.browser.ynSuggestUrl = suggestUrl
- win.messageinfos.init('random', win.yoonoGlob.gStrbundle.getString("random.messageinfos"), yoonoRandomGlob.browser.ynSuggestUrlFrom);
- document.location = suggestUrl;
- }
-
- } catch(e) {
- YOONO_LOG.exception(e);
- }
- }
-
-
- // Nettoyage des suggestions :
- // On supprime des suggestions retournées par le serveur toutes celles figurant deja dans les
- // bookmarks
- yoonoRandomGlob.ynCleanSuggestions = function(suggestions) {
- var tmpurlList = [];
-
- // history urls must be refreshed each time
- var url = '';
- // delete knowurls (found in bkms or history)
- for each (var link in suggestions) {
- url = link.@url.toString();
- if (!yoonoRandomGlob.urlTable[url]) {
- tmpurlList.push(link);
- }
- }
- return tmpurlList;
- }
-